package com.asion.qkan.ui.fragment.categoryfragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.asion.qkan.R;
import com.asion.qkan.adapter.AllDatasAdapter;
import com.asion.qkan.db.Cartoon;
import com.asion.qkan.db.CartoonDao;
import com.asion.qkan.entry.CategoryEntry;
import com.asion.qkan.jiguangtuisong.ExampleApplication;
import com.asion.qkan.ui.contract.FindContract;
import com.asion.qkan.ui.fragment.BaseFragment;
import com.asion.qkan.ui.model.FindModel;
import com.asion.qkan.ui.presenter.CategoryPresenter;
import com.asion.qkan.ui.util.ToastUtil;
import com.asion.qkan.ui.view.AllActivity;

import org.greenrobot.greendao.query.Query;

import java.util.List;

import butterknife.BindView;

/**
 * Created by gino on 16-10-13.
 */

public class AllFragment extends BaseFragment<CategoryPresenter,FindModel> implements FindContract.AllView {
    @BindView(R.id.subcategory_item_recycler)
    RecyclerView recyclerView;
    @BindView(R.id.subcategory_item_swipe)
    SwipeRefreshLayout swipe;
    private AllDatasAdapter adapter;
    private String title;
    private List<CategoryEntry.DataBean.TopicsBean> topics;
    @Override
    protected int getLayoutResource() {
        return R.layout.subcategory_item;
    }

    public static AllFragment newInstance(String s){
        Bundle bundle = new Bundle();
        bundle.putString("title",s);
        AllFragment allFragment = new AllFragment();
        allFragment.setArguments(bundle);
        return allFragment;
     }
    @Override
    public void initPresenter() {
       mPresenter.setVM(this,mModel);
        switch (title) {
            case "全部":
                mPresenter.setAllCategoryDatas(0);
                break;
            case "恋爱":
                mPresenter.setAllCategoryDatas(20);
                break;
            case "爆笑":
                mPresenter.setAllCategoryDatas(24);
                break;
            case "奇幻":
                mPresenter.setAllCategoryDatas(22);
                break;
            case "恐怖":
                mPresenter.setAllCategoryDatas(32);
                break;
            case "耽美":
                mPresenter.setAllCategoryDatas(36);
                break;
            case "剧情":
                mPresenter.setAllCategoryDatas(27);
                break;
            case "成人":
                mPresenter.setAllCategoryDatas(23);
                break;
            case "日常":
                mPresenter.setAllCategoryDatas(44);
                break;
            case "治愈":
                mPresenter.setAllCategoryDatas(19);
                break;
            case "百合":
                mPresenter.setAllCategoryDatas(45);
                break;
            case "三次元":
                mPresenter.setAllCategoryDatas(41);
                break;

        }
        stopProgressDialog();
    }

    @Override
    protected void initView() {
        startProgressDialog();
        Bundle bundle = getArguments();
        title = bundle.getString("title");
        adapter  = new AllDatasAdapter(getContext());
        recyclerView.setAdapter(adapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        adapter.removeAll(topics);
                        adapter.addAll(topics);
                        mhandler.sendEmptyMessage(1);
                    }
                }, 3000);
            }
        });

        adapter.setOnClickListener(bean -> {
            Bundle bundble = new Bundle();
            bundble.putString("id", String.valueOf(bean.getId()));
            bundble.putString("title",bean.getTitle());
            bundble.putString("vertical_image_url",bean.getVertical_image_url());
            AllActivity.startAction(getActivity(),bundble);
        });

        adapter.setOnimageClickListener((v,entry)->{
            Cartoon cartoon = new Cartoon((long) entry.getId(), entry.getTitle(), entry.getUser().getNickname(), entry.getCover_image_url());
            Query<Cartoon> build = ExampleApplication.getCartoonDao().queryBuilder().where(CartoonDao.Properties.Id.eq(cartoon.getId())).build();
            List<Cartoon> list = build.list();
            if (list.contains(cartoon.getId())) {
                v.setImageResource(R.drawable.watch);
                ToastUtil.showShort("已取消关注");
                ExampleApplication.getCartoonDao().delete(cartoon);
            }else if (!list.contains(cartoon.getId())){
                v.setImageResource(R.drawable.unwatch);
                ToastUtil.showShort("关注成功");
                ExampleApplication.getCartoonDao().insert(cartoon);
            }
        });

    }
    private Handler mhandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    swipe.setRefreshing(false);
                    adapter.notifyDataSetChanged();
                    Toast.makeText(getContext(), "刷新成功", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @Override
    public void showDataSuccess(CategoryEntry categoryEntry) {
        List<CategoryEntry.DataBean.TopicsBean> topics = categoryEntry.getData().getTopics();
        this.topics = topics;
        adapter.addAll(topics);
    }

    @Override
    public void showDataError(Throwable throwable) {

    }
}
